package mytest.util;

import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import test.service.ServiceTestModel;
import test.testcase.TestModelStep;
import test.testcase.dao.TestModelDao;
import test.utils.JsonResult;

public class CheckAddToModel {
	
	private List<String> list;
	
	public CheckAddToModel(String id) {
		list = new ArrayList<>();
		list.add(id);
	}
	
	public boolean canAddToModel(String id) {
		if (list.contains(id)) {
			return false;
		}else {
			list.add(id);
			JSONArray array = TestModelDao.getTestModelSteps(id, null).read("data").toJsonArray();
			if (array != null) {
				for(int i=0;i<array.length();i++) {
					TestModelStep modelstep = ServiceTestModel.getTestModelStep(array.getJSONObject(i).getString("id"));
					switch (modelstep.getFunctionname()) {
					case "model":
						return canAddToModel(modelstep.getOperateid());
					default:
						break;
					}
				}
			}
		}
		return true;
	}
	
	public static String getLastModelStepId() {
		JsonResult result = TestModelDao.getLastModelStepId();
		return result.read("data[0].ID").toString();
	}

}
